home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / dlists / mountain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.3 KB  |  163 lines

  1. /* Copyright 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* mountain.c -    Draws a fractal mountain.
  18.  * Adapted from code written by Benedikt Kessler, SGI Munich
  19.  */
  20.  
  21. #include <GL/gl.h>
  22. #include <GL/glu.h>
  23. #include <math.h>
  24.  
  25. /*  Function Prototypes  */
  26.  
  27. GLvoid computeMountain( GLvoid );
  28. GLvoid drawMountain( GLvoid );
  29. GLvoid setView( GLvoid );
  30.  
  31. #define SIZE         128        /* Size means the size of the matrix */
  32. #define SIZE1         (SIZE + 1)
  33. #define MEMSIZE     ((SIZE1*(SIZE1+1))>>1)
  34. #define LOG2SIZE     7
  35.  
  36. #define p(i,j) ( p[ SIZE1*(i) - ((i)*((i)-1) >> 1) + (j) ] )
  37. #define n(i,j) ( n[ SIZE1*(i) - ((i)*((i)-1) >> 1) + (j) ] )
  38.  
  39. /* The triangle matrix used has following gestalt:
  40.  *   ------       
  41.  *   |    /
  42.  *   |   /
  43.  *   |  /
  44.  *   | /
  45.  *   |/
  46.  *   
  47.  * To save storage it's linearizised.  Therefore you need the ugly 
  48.  * macros defined above.
  49.  *
  50.  * The matrix p[] holds the high values of each vertex of the mountain, 
  51.  * n[] holds the normal vectors. 
  52.  *        
  53.  */
  54.  
  55. /* private variables */
  56. static short p[MEMSIZE];    /* Contains highlevel values for mountain */
  57. static float n[MEMSIZE][3];
  58. static float look_x,look_y,look_z; /* Position of eye point for glulookat */
  59.  
  60. static float s_params[]= { 0.0, 0.0, 0.0, 0.0 };        
  61. static float t_params[]= { 0.0, 1.0/70.0, 0.0, 0.3 };
  62.  
  63. GLvoid
  64. drawMountain(GLvoid)
  65. {
  66.         static float v[3];
  67.         register long i=0,j=0,k=0;
  68.  
  69.  
  70.         glPushMatrix();
  71.                 gluLookAt(look_x,look_y,look_z,
  72.               SIZE/2.0,-SIZE/3.0,SIZE/2.0,
  73.               0,1,0);
  74.                 v[0] = 0.0; 
  75.         v[1] = 1.0; 
  76.         v[2] = 0.0;
  77.  
  78.                 glNormal3fv(&(n(i,j)[0]));
  79.                 for(i=0; i<SIZE; i++)
  80.                 {
  81.                         glBegin( GL_TRIANGLE_STRIP );
  82.                 for(j=SIZE-i;j>0;j--)
  83.                 {
  84.                     v[0]=(float)i;
  85.                     v[1]=(float)p(i,j)/100.0;
  86.                     v[2]=(float)j;
  87.                     glNormal3fv(n(i,j));
  88.                     glVertex3fv(v);
  89. #ifdef DEBUG
  90.                     fprintf(stdout,
  91.                     "v[%d][%d] = (%f, %f, %f)\n",
  92.                     i,j,v[0],v[1],v[2]);
  93. #endif
  94.  
  95.                     v[0]=(float)i+1;
  96.                     v[1]=(float)p(i+1,j-1)/100.0;
  97.                     v[2]=(float)j-1;
  98.                     glVertex3fv(v);
  99.                 }
  100.                 v[0]=i;
  101.                 v[1]=p(i,0)/100.0; v[2]=0.0;
  102.                         glVertex3fv(v);
  103.                         glEnd();
  104.                 }
  105.  
  106.         glPopMatrix();
  107. }
  108.  
  109. GLvoid 
  110. setView( GLvoid )
  111. {
  112.         look_x = -16.0;
  113.         look_y = 192.0;
  114.         look_z = -16.0;
  115. }
  116.  
  117. GLvoid 
  118. computeMountain( GLvoid )
  119. {
  120.         register long k,i,j,w2,b,w;
  121.         float br;
  122.         float d=.5,e=1;
  123.         float ay, by, nx, nz, len;
  124.  
  125.         w=SIZE;
  126.         for (i=0; i<MEMSIZE; i++)
  127.         p[i]=0;
  128.         p(SIZE/9,SIZE/3)=120;
  129.         for(k=0; k<LOG2SIZE; k++)
  130.         {
  131.                 br=e*w*100; w2=w>>1;
  132.  
  133.                 for(j=0;j<SIZE;j+=w)
  134.                 {
  135.                         for(i=0;i<SIZE-j;i+=w)
  136.                         {
  137.                                 b=(p(i,j)+p(i+w,j))>>1;
  138.                                 p(i+w2,j)+=(b+(rand()/32768.0-d)*br);
  139.                                 b=(p(j,i)+p(j,i+w))>>1;
  140.                                 p(j,i+w2)+=(b+(rand()/32768.0-d)*br);
  141.                                 b=(p(SIZE-j-i,i)+p(SIZE-j-i-w,i+w))>>1;
  142.                                 p(SIZE-j-i-w2,i+w2)+=(b+(rand()/32768.0-d)*br);
  143.                         }
  144.                 }
  145.                 w>>=1;
  146.         }
  147.  
  148.         /* Now compute normals */
  149.         for(i=0; i<SIZE; i++)
  150.         {
  151.                 for(j=SIZE-i;j>0;j--)
  152.                 {
  153.                         ay = p(i,j-1)-p(i,j);
  154.                         by = p(i+1,j-1)-p(i,j);
  155.                         nx = -ay; nz = ay-by; 
  156.                         len = sqrtf(nx*nx + 1.0f + nz*nz);
  157.                         n(i,j)[0] = nx/len;
  158.             n(i,j)[1] = -1.0f/len; 
  159.             n(i,j)[2] = nz/len;
  160.                 }
  161.         }
  162. }
  163.